﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using SemWeb;
using SemWeb.Stores;
using SemWeb.Util;
using SemWeb.Query;

namespace Semweb
{
  class Program
  {
    static void Main(string[] args)
    {
      const string xsdPrefix = "http://www.w3.org/2001/XMLSchema#";
      const string xsdDateTime = xsdPrefix + "dateTime";
      const string rdfPrefix = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
      const string dmPrefix = "http://dm.rdlfacade.org/data#";
      const string rdlPrefix = "http://rdl.rdlfacade.org/data#";
      const string tplPrefix = "http://tpl.rdlfacade.org/data#";
      const string egPrefix = "http://www.example.com/data#";
      Store store = Store.Create(@"sqlserver:rdf:Database=rdf;data source=.\SQLEXPRESS; Initial Catalog=iring; User Id=iring; Password=iring;");

      store.Clear();

      BNode bNoneClassificationTemplate = new BNode("c1");
      BNode bNoneClassificationTemplate2 = new BNode("c2");
      string rdfType = rdfPrefix + "type";
      Entity classificationTemplateType = dmPrefix + "classification";
      Statement statement1 = new Statement(bNoneClassificationTemplate, rdfType, classificationTemplateType);
      Statement statement6 = new Statement(bNoneClassificationTemplate2, rdfType, classificationTemplateType);

      Entity classId = rdlPrefix + "R34026677432";
      string classType = dmPrefix + "class";
      Statement statement2 = new Statement(bNoneClassificationTemplate, classType, classId);
      Statement statement7 = new Statement(bNoneClassificationTemplate2, classType, classId);

      string instanceType = dmPrefix + "instance";
      Entity instanceValue = egPrefix + "id__STG-C104001-02-09";
      Statement statement3 = new Statement(bNoneClassificationTemplate, instanceType, instanceValue);
      Statement statement8 = new Statement(bNoneClassificationTemplate2, instanceType, instanceValue);

      string startDateTime = tplPrefix + "startDateTime";
      Literal startDateTimeValue = new Literal("08/04/2009 12:00 PM EST", null, xsdDateTime);
      Literal startDateTimeValue2 = new Literal("08/05/2009 12:00 PM EST", null, xsdDateTime);
      Statement statement4 = new Statement(bNoneClassificationTemplate, startDateTime, startDateTimeValue);
      Statement statement9 = new Statement(bNoneClassificationTemplate2, startDateTime, startDateTimeValue2);

      string endDateTime = tplPrefix + "endDateTime";
      Literal endDateTimeValue = new Literal("08/05/2009 12:00 PM EST", null, xsdDateTime);
      Statement statement5 = new Statement(bNoneClassificationTemplate, endDateTime, endDateTimeValue);

      store.Add(statement1);
      store.Add(statement2);
      store.Add(statement3);
      store.Add(statement4);
      store.Add(statement5);
      store.Add(statement6);
      store.Add(statement7);
      store.Add(statement8);
      store.Add(statement9);

      GraphMatch query = new GraphMatch();

      Variable classificationTemplateVariable = new Variable("c");
      Statement statementA = new Statement(classificationTemplateVariable, rdfType, classificationTemplateType);
      query.AddGraphStatement(statementA);

      Statement statementB = new Statement(classificationTemplateVariable, classType, classId);
      query.AddGraphStatement(statementB);

      Statement statementC = new Statement(classificationTemplateVariable, instanceType, instanceValue);
      query.AddGraphStatement(statementC);

      QueryResultBuffer resultBuffer = new QueryResultBuffer();
      query.Run(store, resultBuffer);

      //Entity[] subjects = new Entity[resultBuffer.Bindings.Count];
      //int bindingIndex = 0;
      //foreach (VariableBindings binding in resultBuffer.Bindings)
      //{
      //  int variableIndex = 0;
      //  foreach (Variable variable in binding.Variables)
      //  {
      //    if (variable.LocalName == "c")
      //    {
      //      subjects[bindingIndex] = (Entity)binding.Values[variableIndex];
      //    }
      //    variableIndex++;
      //  }
      //  bindingIndex++;
      //}

      //SelectFilter filter = new SelectFilter();
      //filter.Subjects = subjects;
      //filter.Predicates = new Entity[] { endDateTime };
      //SelectResult result = store.Select(filter);

      Variable endDateTimeVariable = new Variable("endDateTime");
      Statement statementD = new Statement(classificationTemplateVariable, endDateTime, endDateTimeVariable);
      query.AddGraphStatement(statementD);
      
      resultBuffer = new QueryResultBuffer();
      //query.AddLiteralFilter(endDateTimeVariable, LiteralFilter.Create(LiteralFilter.CompType.NULL, null));
      query.Run(store, resultBuffer);

      Console.WriteLine("Store count : " + store.StatementCount);
      Console.ReadLine();

      store.Dispose();
    }

    private static void DisplayStatement(Statement[] statements)
    {
      Console.WriteLine("------------------------------------------\r\n");

      foreach (Statement statement in statements)
      {
        Console.WriteLine("Subject: " + statement.Subject + "\r\n" +
            "Predicate: " + statement.Predicate + "\r\n" +
            "Object: " + statement.Object + "\r\n"
           );
      }
      Console.WriteLine("------------------------------------------\r\n");
      Console.ReadLine();
    }
  }
}
